﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;

namespace AutoBuyWinform
{
    public partial class Form1 : Form
    {
        public static string webUrl;
        public static string chatUrl;
        public static string yjSendMsgUrl;
        public static string yjUpdateMsgUrl;
        public static string loginUrl;
        public static string captchaUrl;
        public static string postToCartUrl;
        public static string createOrderUrl;
        public static string yjPageUrl;
        public static string goodsUrl;
        public static string cartUrl;
        public static int yj1;
        public static int yj2;
        public static int yj3;
        public static int startBuy_h;
        public static int startBuy_m;
        public static int startbuy_s;
        public static int waitTime;
        public static int timeLength;
        private string loginName;
        private string pwd;

        private bool isLogin = false;
        //private Logs logs;
        private Operations operations;
        private CookieContainer cc = new CookieContainer();
        private CookieCollection ccReturned;

        //public List<Fq> fqList;
        //public class Fq{
        //    public Fq(String fqmc, String fqdm)
        //    {
        //        this.fqmc = fqmc;
        //        this.fqdm = fqdm;
        //    }
        //    private String fqmc{get;set;}
        //    private String fqdm{get;set;}
        //};

        public Form1()
        {
            InitializeComponent();

            isLogin = false;
            //logs = new Logs(false);
            operations = new Operations();

            //fqList = new List<Fq>();
            //fqList.Add(new Fq("特价商品", "210"));
            //fqList.Add(new Fq("疯狂抢购", "220"));
            //fqList.Add(new Fq("交易A区", "40"));
            //fqList.Add(new Fq("优品区", "60"));
            //fqList.Add(new Fq("梅钢专区", "90"));
            //fqList.Add(new Fq("宁钢专区", "80"));
            //fqList.Add(new Fq("生僻特价区", "70"));
            
        }

        private void initUrl()
        {
            webUrl = textWebUrl.Text;
            if (webUrl == null || webUrl == "")
            {
                MessageBox.Show("请输入主页URL");
                return;
            }

            //webUrl = "http://fdc.baostar.com";
            chatUrl = "http://chat.baostar.com";
            yjSendMsgUrl = chatUrl + "/message/new";
            yjUpdateMsgUrl = chatUrl + "/message/updates";
            loginUrl = webUrl;
            captchaUrl = webUrl + "/captcha.gif?t=" + ((Int64)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString();
            postToCartUrl = webUrl + "/shopping/cart/packs/add/";
            createOrderUrl = webUrl + "/order/create_contract_without_bargaining/";
            cartUrl = webUrl + "/shopping/my-carts/";
            yjPageUrl = webUrl + "/shopping/group-number/new/";
        }

        //用于实现CookieContainer的复制操作
        private CookieContainer GetCookieContainerCopy(CookieContainer CC)
        {
            CookieContainer newCC = new CookieContainer();
            newCC.Capacity = cc.Capacity;
            newCC.MaxCookieSize = cc.MaxCookieSize;
            newCC.PerDomainCapacity = cc.PerDomainCapacity;
            newCC.Add(new Uri(webUrl), operations.InitCookieCollection(CC.GetCookies(new Uri(webUrl))));

            return newCC;
        }  

        private void btnLogin_Click(object sender, EventArgs e)
        {
            initUrl();

            loginName = textLoginName.Text;
            pwd = textPwd.Text;
            if (Util.isEmpty(loginName) || Util.isEmpty(pwd))
            {
                MessageBox.Show("请输入用户名密码");
            }

            //MessageBox.Show(((Int64)(DateTime.Now-new DateTime(1970,1,1)).TotalMilliseconds).ToString());

            string veriCode = operations.GetVeriCode(loginUrl,captchaUrl, GetCookieContainerCopy(cc), out ccReturned);
            cc.Add(ccReturned);
            isLogin = operations.Login(loginUrl, loginName, pwd, veriCode, GetCookieContainerCopy(cc), out ccReturned);
            cc.Add(ccReturned);

            //yj_hidden
            //YjPage yjPage = new YjPage();
            //foreach (Cookie cookieValue in cc.GetCookies(new Uri(Form1.webUrl)))
            //{
            //    yjPage.addCookie(cookieValue);
            //}
            //yjPage.resetUrl(Form1.cartUrl);
            //yjPage.resetTitle(Form1.cartUrl);
            //yjPage.Visible = true;
        }

        private void btnBuy_Click(object sender, EventArgs e)
        {
            if (!isLogin)
            {
                MessageBox.Show("请先登录");
                return;
            }

            goodsUrl = textGoodsUrl.Text;
            if (Util.isEmpty(goodsUrl))
            {
                MessageBox.Show("请输入商品页面网址");
                return;
            }

            if (Int32.TryParse(textYj1.Text, out yj1))
            {
                yj1 = Int32.Parse(textYj1.Text);
            }
            else
            {
                MessageBox.Show("\"议价\"1.数字格式错误");
                return;
            }

            if (Int32.TryParse(textYj1.Text, out yj2))
            {
                yj2 = Int32.Parse(textYj1.Text);
            }
            else
            {
                MessageBox.Show("\"议价\"2.数字格式错误");
                return;
            }

            if (Int32.TryParse(textYj1.Text, out yj3))
            {
                yj3 = Int32.Parse(textYj1.Text);
            }
            else
            {
                MessageBox.Show("\"议价\"3.数字格式错误");
                return;
            }

            

            if (Int32.TryParse(textWaitTime.Text, out waitTime))
            {
                waitTime = Int32.Parse(textWaitTime.Text);
            }
            else
            {
                MessageBox.Show("\"抢拍间隔\"数字格式错误");
                return;
            }

            if (Int32.TryParse(textTimeLength.Text, out timeLength))
            {
                timeLength = Int32.Parse(textTimeLength.Text);
            }
            else
            {
                MessageBox.Show("\"抢拍时长\"数字格式错误");
                return;
            }

            if (Int32.TryParse(textStartBuy_h.Text, out startBuy_h))
            {
                startBuy_h = Int32.Parse(textStartBuy_h.Text);
            }
            else
            {
                MessageBox.Show("\"开始扫描时间\"数字格式错误");
                return;
            }

            if (Int32.TryParse(textStartBuy_m.Text, out startBuy_m))
            {
                startBuy_m = Int32.Parse(textStartBuy_m.Text);
            }
            else
            {
                MessageBox.Show("\"开始扫描时间\"数字格式错误");
                return;
            }

            if (Int32.TryParse(textStartBuy_s.Text, out startbuy_s))
            {
                startbuy_s = Int32.Parse(textStartBuy_s.Text);
            }
            else
            {
                MessageBox.Show("\"开始扫描时间\"数字格式错误");
                return;
            }


            LastTimeForm lastTimeForm = new LastTimeForm(GetCookieContainerCopy(cc));
            lastTimeForm.Visible = true;
            lastTimeForm.startLastTime();

            //string page = Util.GetPage(goodsUrl,GetCookieContainerCopy(cc),out ccReturned);
            //cc.Add(ccReturned);
            //Product product = operations.GetProduct(page);
            
            //if (product == null)
            //{
            //    MessageBox.Show("没有找到商品信息");
            //    return;
            //}
            //operations.AddToCart(product, GetCookieContainerCopy(cc));
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void btnStopBuy_Click(object sender, EventArgs e)
        {
            Application.Exit();
            //foreach(Thread t in operations.getThreadContainer())
            //{
            //    if (t != null && t.IsAlive)
            //    {
            //        t.Abort();
            //    }
            //}
        }

        private void btnBd_Click(object sender, EventArgs e)
        {
            YjPage yjPage = new YjPage();
            yjPage.resetUrl("http://www.baidu.com");
            yjPage.Visible = true;
        }

        

    }
}
